//
// Created by HP on 2024/1/8.
//

#include "aurora_ui.h"

#include "rtthread.h"

Aurora_UI::Aurora_UI(){
    select_motor = MODE_LEFT_MOTOR;
    mode_sel = MODE_HALT_MOTOR;
}

void Aurora_UI::addEncoderKey_Driver(EncoderKey_Driver *encoderkey_s){
    encoderkey = encoderkey_s;
}
void Aurora_UI::add_Driver(LCD_Driver *lcd_s,Aurora_Font *font_s){
    lcd = lcd_s;
    font = font_s;

}
void Aurora_UI::addLCD_Driver(LCD_Driver *lcd_s){
    lcd = lcd_s;
}
void Aurora_UI::addFont_Driver(Aurora_Font *font_s){
    font = font_s;
}

void Aurora_UI::switch_to_menu(){
    curr_page = menu_page;
    lcd->fill(0xffff);
    menu = new Aurora_Menu(lcd,font);
    menu->Init();
    menu->switch_prev_label();
}
void Aurora_UI::dealKeyEvent(){
    EncoderKeyState encoderKeyState = encoderkey->get_key_state();
    if( encoderKeyState != EncoderKey_None ){
        send_halt_motor();   //先暂停电机
        rt_thread_mdelay(50);
        switch (curr_page) {
            case menu_page:
                dealKeyEventIN_menu(encoderKeyState);
                break;
            case mode_page:
                dealKeyEventIN_mode(encoderKeyState);
                break;
            case wave_page:
                dealKeyEventIN_wave(encoderKeyState);
                break;
            case detail_page:
                dealKeyEventIN_detail(encoderKeyState);
                break;
            case standby_page:
                dealKeyEventIN_img(encoderKeyState);
                break;
            default:
                break;
        }
        send_recovery_motor();  //恢复电机状态
       // usb_printf("usb state  %d\r\n",encoderKeyState);
    }
}
void Aurora_UI::dealKeyEventIN_menu(EncoderKeyState encoderKeyState){
    switch (encoderKeyState){
        case EncoderKey_Left:
            menu->switch_prev_label();
            break;
        case EncoderKey_Right:
            menu->switch_next_label();
            break;
        case EncoderKey_Click:
            {
                uint8_t index = menu->get_curr_menu();
                if( index == mode_page ){
                    curr_page = mode_page;
                    lcd->fill(0xffff);
                    delete menu;
                    mode = new Aurora_Mode(lcd,font,select_motor,mode_sel);
                    mode->mode_label_init();
                    mode_sel = mode->get_mode_sel();
                    select_motor = mode->get_select_motor();
                }
                else if(index == wave_page){
                    curr_page = wave_page;
                    lcd->fill(0xffff);
                    delete menu;
                    wave = new Aurora_Wave(lcd,font);
                    wave->wave_label_init();
                }
                else if( index == detail_page){
                    curr_page = detail_page;
                    lcd->fill(0xffff);
                    delete menu;
                    detail = new Aurora_Info(lcd,font);
                    detail->Init();
                }
                else if( index == standby_page){
                    curr_page = standby_page;
                    lcd->fill(0xffff);
                    delete menu;
                    img = new Aurora_Img(lcd,font);
                    img->show_prev_img();
                }
            }
            break;
        case EncoderKey_DClick:
            break;
        case EncoderKey_LClick:
            HAL_NVIC_SystemReset();
            break;
        default:
            break;
    }
}
void Aurora_UI::dealKeyEventIN_mode(EncoderKeyState encoderKeyState){
    switch (encoderKeyState){
        case EncoderKey_Left:
            mode->switch_prev_mode();
            mode_sel = mode->get_mode_sel();
            select_motor = mode->get_select_motor();
            break;
        case EncoderKey_Right:
            mode->switch_next_mode();
            mode_sel = mode->get_mode_sel();
            select_motor = mode->get_select_motor();
            break;
        case EncoderKey_Click:
            mode->switch_sel_motor();
            mode_sel = mode->get_mode_sel();
            select_motor = mode->get_select_motor();
            break;
        case EncoderKey_DClick:
            mode_sel = mode->get_mode_sel();
            select_motor = mode->get_select_motor();
            delete mode;
            switch_to_menu();
            break;
        case EncoderKey_LClick:
            mode->calibration_motor();
            mode_sel = mode->get_mode_sel();
            select_motor = mode->get_select_motor();
            break;
        default:
            break;
    }
}
void Aurora_UI::dealKeyEventIN_wave(EncoderKeyState encoderKeyState){
    switch (encoderKeyState){
        case EncoderKey_Left:
            wave->switch_prev_wave();
            break;
        case EncoderKey_Right:
            wave->switch_next_wave();
        case EncoderKey_Click:
            break;
        case EncoderKey_DClick:
            delete wave;
            switch_to_menu();
            break;
        case EncoderKey_LClick:
            break;
        default:
            break;
    }
}
void Aurora_UI::dealKeyEventIN_detail(EncoderKeyState encoderKeyState){
    switch (encoderKeyState){
        case EncoderKey_Left:
            break;
        case EncoderKey_Right:
        case EncoderKey_Click:
            break;
        case EncoderKey_DClick:
            delete detail;
            switch_to_menu();
            break;
        case EncoderKey_LClick:
            break;
        default:
            break;
    }
}
void Aurora_UI::dealKeyEventIN_img(EncoderKeyState encoderKeyState){
    switch (encoderKeyState){
        case EncoderKey_Left:
            img->show_prev_img();
            break;
        case EncoderKey_Right:
            img->show_next_img();
        case EncoderKey_Click:
            break;
        case EncoderKey_DClick:
            delete img;
            switch_to_menu();
            break;
        case EncoderKey_LClick:
            break;
        default:
            break;
    }
}

//处理来着于 FOC线程的邮箱
void Aurora_UI::dealmsgEvent_from_FOC(struct Aurora_FOC2UI_MSG *foc2ui_msg_p){
    if(foc2ui_msg_p->type == update_wave){
        wave->addWaveData(foc2ui_msg_p->wave_data);
    }
    else if(foc2ui_msg_p->type == update_detail){
        detail->updateDetail(foc2ui_msg_p->detail_data);
    }
    else if( foc2ui_msg_p->type == save_focL_param){
        font->Save_FOC_Parameter(LEFT_MOTOR_ID,foc2ui_msg_p->foc_param);
    }
    else if( foc2ui_msg_p->type == save_focR_param ){
        font->Save_FOC_Parameter(RIGHT_MOTOR_ID,foc2ui_msg_p->foc_param);
    }

}

//处理来着于 COMM线程的邮箱
void Aurora_UI::dealmsgEvent_from_COMM(struct Aurora_COMM2UI_MSG *comm2ui_msg_p){
    if(comm2ui_msg_p->type == file_update) {
        //当前为命令 : 删除对应地址里面的内容，并且设置更新的起始地址
        if (comm2ui_msg_p->cmd == IS_CMD) {
            if (comm2ui_msg_p->file_update.file_type == CMD_FONT_BIG) {
                font->delete_font_data(Aurora_Font::FONT_SIZE::font_size_big);
                font->set_update_font_type(Aurora_Font::FONT_SIZE::font_size_big);
                font->set_update_type(is_fontdata);
                ui_param.font_big_flag = UI_DISENABLE;
            } else if (comm2ui_msg_p->file_update.file_type == CMD_FONT_MIDDLE) {
                font->delete_font_data(Aurora_Font::FONT_SIZE::font_size_middle);
                font->set_update_font_type(Aurora_Font::FONT_SIZE::font_size_middle);
                font->set_update_type(is_fontdata);
                ui_param.font_middle_flag = UI_DISENABLE;
            } else if (comm2ui_msg_p->file_update.file_type == CMD_FONT_SMALL) {
                font->delete_font_data(Aurora_Font::FONT_SIZE::font_size_small);
                font->set_update_font_type(Aurora_Font::FONT_SIZE::font_size_small);
                font->set_update_type(is_fontdata);
                ui_param.font_small_flag = UI_DISENABLE;
            }
            else if( comm2ui_msg_p->file_update.file_type == CMD_ANIM_IMG){
                font->delete_aim_img_data();
                font->set_update_type(is_aim_imgdata);
                ui_param.aim_flag = UI_ENABLE;
                ui_param.aim_num  = comm2ui_msg_p->file_update.file_size;
            }
            else if( comm2ui_msg_p->file_update.file_type == CMD_IMG){
                font->delete_img_data();
                font->set_update_type(is_imgdata);
                ui_param.img_flag = UI_ENABLE;
                ui_param.img_num  = comm2ui_msg_p->file_update.file_size;
            }
            font->Save_UI_Parameter(ui_param); // 保存参数
        }
        else if(comm2ui_msg_p->cmd == IS_DATA){
            if(font->get_update_type() == is_fontdata)
                font->set_update_font_data(comm2ui_msg_p->file_update.file_data,comm2ui_msg_p->file_update.file_data_len);
            else if(font->get_update_type() == is_aim_imgdata)
                font->set_update_aim_img_data(comm2ui_msg_p->file_update.file_data,comm2ui_msg_p->file_update.file_data_len);
            else if( font->get_update_type() == is_imgdata)
                font->set_update_img_data(comm2ui_msg_p->file_update.file_data,comm2ui_msg_p->file_update.file_data_len);


            if( comm2ui_msg_p->file_update.file_last == ENABLE ){
                comm2ui_msg_p->file_update.file_last = DISABLE;
                if( comm2ui_msg_p->file_update.file_type == CMD_FONT_BIG )
                    ui_param.font_big_flag = UI_ENABLE;
                else if( comm2ui_msg_p->file_update.file_type == CMD_FONT_MIDDLE )
                    ui_param.font_middle_flag = UI_ENABLE;
                else
                    ui_param.font_small_flag = UI_ENABLE;
                font->Save_UI_Parameter(ui_param); // 保存参数
            }
        }
    }
    else if( comm2ui_msg_p->type == mstate_update){
        select_motor = comm2ui_msg_p->mstate_update.select_motor;
        mode_sel     = comm2ui_msg_p->mstate_update.mode_sel;
    }
}

/*加载参数*/
void Aurora_UI::load_ui_parameter(){
    ui_param = font->Load_UI_Parameter();
}
/*开机动画*/
void Aurora_UI::boot_animation(){
    rt_interrupt_enter();
    uint8_t i;
    uint16_t j;
    uint32_t offsize = 0;
    uint8_t start_y = 0;
    if(ui_param.aim_flag == UI_ENABLE) {
        for (i = 0; i < ui_param.aim_num; i++) {

            font->getaimimgData(offsize, (uint8_t *) lcd_buff, 320 * 160);
            offsize += 320 * 160;
            while (HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
            LCD_CS_H;
            lcd->draw_rectangle_Aim(0, start_y, 320, start_y + 80, lcd_buff);
            start_y += 80;

            font->getaimimgData(offsize, (uint8_t *) lcd_buff, 320 * 160);
            offsize += 320 * 160;
            while (HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
            LCD_CS_H;
            lcd->draw_rectangle_Aim(0, start_y, 320, start_y + 80, lcd_buff);
            start_y += 80;

            font->getaimimgData(offsize, (uint8_t *) lcd_buff, 320 * 24);
            offsize += 320 * 24;
            while (HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
            LCD_CS_H;
            lcd->draw_rectangle_Aim(0, start_y, 320, start_y + 12, lcd_buff);
            start_y = 0;
            rt_thread_mdelay(100);
        }
    }

    lcd->fill(0xabcd);
    for(j=0;j<10*172;j++)
        lcd_buff[j] = 0xFFFF;
    for(i=0;i<16;i++){
        lcd->draw_rectangle((15-i)*10,0,(15-i+1)*10,172);
        lcd->draw_rectangle((16+i)*10,0,(16+i+1)*10,172);
        rt_thread_mdelay(80);
    }
    switch_to_menu();
    rt_interrupt_leave();
}


//暂停电机
void Aurora_UI::send_halt_motor(){
    ui2foc_msg.type = enable_mode_LRmotor;
    ui2foc_msg.foc_mode = HALT_MOTOR;
    ui2foc_msg.cmd = IS_CMD;
}
//回复电机状态
void Aurora_UI::send_recovery_motor(){
    if (select_motor == MODE_LEFT_MOTOR)
        ui2foc_msg.type = enable_mode_Lmotor;
    else if (select_motor == MODE_RIGHT_MOTOR)
        ui2foc_msg.type = enable_mode_Rmotor;
    else
        ui2foc_msg.type = enable_mode_LRmotor;
    if (mode_sel == MODE_Calibration_MOTOR)
        ui2foc_msg.foc_mode = Calibration_MOTOR;
    else if (mode_sel == MODE_HALT_MOTOR)
        ui2foc_msg.foc_mode = HALT_MOTOR;
    else if (mode_sel == MODE_OPEN_MOTOR)
        ui2foc_msg.foc_mode = OPEN_LOOP_MOTOR;
    else if (mode_sel == MODE_I_MOTOR)
        ui2foc_msg.foc_mode = I_LOOP_MOTOR;
    else if (mode_sel == MODE_SPEED_MOTOR)
        ui2foc_msg.foc_mode = SPEED_LOOP_MOTOR;
    else if (mode_sel == MODE_POSITION_MOTOR)
        ui2foc_msg.foc_mode = POSITION_LOOP_MOTOR;
    ui2foc_msg.cmd = IS_CMD;
}